package com.hiss.basic.system.helper;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.MD5;
import cn.hutool.extra.spring.SpringUtil;
import com.hiss.basic.client.system.ISysParameterService;
import com.hiss.basic.model.system.response.SysParameterQueryResponse;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class SysUserPasswordHelper {

    private static final ISysParameterService sysParameterService = SpringUtil.getBean(ISysParameterService.class);

    private static final String PARAMETER_KEY = "default_password";

    private static final String DEFAULT_PASSWORD = "888888";

    public static String encryptPassword() {
        try {
            SysParameterQueryResponse response = sysParameterService.findByKey(PARAMETER_KEY);
            if (ObjectUtil.isNotNull(response)) {
                String value = response.getValue();
                return MD5.create().digestHex(value, "UTF-8");
            }
        } catch (Exception e) {
            log.error("获取默认密码失败", e);
        }
        return MD5.create().digestHex(DEFAULT_PASSWORD, "UTF-8");
    }

    public static String encryptPassword(String password) {
        return MD5.create().digestHex(password, "UTF-8");
    }

    public static boolean verifyPassword(String password, String encryptPassword) {
        return StrUtil.equals(encryptPassword, encryptPassword(password));
    }
}
